#ifndef _UNISTD_H
#define _UNISTD_H

/* lseek */
#define SEEK_SET    0   // 设置文件读写指针
#define SEEK_CUR    1   // 从当前文件读写指针做偏移
#define SEEK_END    2   // 从文件末尾做偏移

#include <sys/stat.h>

#ifdef __LIBRARY__

#define __NR_setup  0
#define __NR_exit   1
#define __NR_fork   2
#define __NR_read   3
#define __NR_write  4
#define __NR_open   5
#define __NR_close  6
#define __NR_creat  8
#define __NR_unlink 10
#define __NR_lseek  19
#define __NR_pause  29
#define __NR_sync   36
#define __NR_dup    41

#define _syscall0(type,name) \
type name(void) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
    : "=a" (__res) \
    : "0" (__NR_##name)); \
if (__res >= 0) \
    return (type) __res; \
errno = -__res; \
return -1; \
}

#define _syscall1(type,name,atype,a) \
type name(atype a) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
    : "=a" (__res) \
    : "0" (__NR_##name),"b" ((long)(a))); \
if (__res >= 0) \
    return (type) __res; \
errno = -__res; \
return -1; \
}

#define _syscall3(type,name,atype,a,btype,b,ctype,c) \
type name(atype a,btype b,ctype c) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
    : "=a" (__res) \
    : "0" (__NR_##name),"b" ((long)(a)),"c" ((long)(b)),"d" ((long)(c))); \
if (__res>=0) \
    return (type) __res; \
errno=-__res; \
return -1; \
}

#endif /* __LIBRARY__ */

extern int errno;

int close(int fildes);
int creat(const char *filename, mode_t mode);
int dup(int fildes);
void _exit(int status);
int lseek(int fildes, off_t offset, int origin);
int open(const char *filename, int flag, ...);
int read(int fildes, char *buf, off_t count);
int unlink(const char *filename);
int write(int fildes, const char *buf, off_t count);

#endif
